<?php

namespace App\Exports\DomainInduce;

use App\Models\Domain\Induce\ExcelQueue;
use App\Models\Domain\Induce\Info as domainInfo;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\Middleware\RateLimited;
use Log;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Concerns\WithCustomQuerySize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\BeforeWriting;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class Info implements FromQuery, WithHeadings, ShouldAutoSize, WithStyles, WithEvents ,ShouldQueue ,WithCustomChunkSize {

    use Exportable;
    private $end_time;

    //设置样式
    public function styles(Worksheet $sheet) {
        $col_count = $sheet->getCellCollection()->getCurrentColumn();
        $row_count = $sheet->getCellCollection()->getCurrentRow();

//        $sheet->mergeCells('A1:E1');  //合并
        //设置所有内容 加上细边框
        $sheet->getStyle('A1:'.$col_count.$row_count)->getBorders()->getAllBorders()->setBorderStyle('thin');

        //https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#valid-array-keys-for-style-applyfromarray   //官方文档
        return [
            1 => [
                'font' => ['bold' => true, 'size' => 14],
                'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER], //居中显示
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'startColor' => [
                        'argb' => \PhpOffice\PhpSpreadsheet\Style\Color::COLOR_YELLOW,    //背景色
                    ],
                ],
//                'borders' => [
//                    'outline' => [
//                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,   // 细边框
//                         'color' => ['argb' => Color::COLOR_BLACK],
//                    ],
//                ],


            ]
        ];
    }

    //设置表头
    public function headings(): array {
        return [['id', 'domain', 'self_self', 'self_price', 'sougou_snapshot', 'sougou_title', 'old_last']];
    }

    //数据字段
    public function query() {
        return domainInfo::query()->where('id', '<', 10000)->select([
            'id', 'domain', 'self_self', 'self_price', 'sougou_snapshot', 'sougou_title', 'old_last'
        ]);
    }


    public function registerEvents(): array {
        return [
            BeforeExport::class => function (BeforeExport $event) {
                //1
                $this->end_time = microtime(true);
                //  dump($event->getConcernable());
                Log::channel('excel')->info('初始化...', ['time' => $this->end_time,]);
            },
            BeforeSheet::class => function (BeforeSheet $event) {
                //2
                //  dump('BeforeSheet'.(microtime(true) - $this->end_time));

                Log::channel('excel')->info('正在创建表格...', ['time' => microtime(true) - $this->end_time]);
            },
            AfterSheet::class => function (AfterSheet $event) {
                //3
                // dump('AfterSheet'.(microtime(true) - $this->end_time));

                Log::channel('excel')->info('表格创建完毕', ['time' => microtime(true) - $this->end_time]);
            },
            BeforeWriting::class => function (BeforeWriting $event) {
                //4
                //  dump('BeforeWriting'.(microtime(true) - $this->end_time));
//                ExcelQueue::where('queue_name', '导出域名')
//                    ->where('type', 2)->update(['current_chunk' => $this->current_count]);

                Log::channel('excel')->info('开始生成文件...', ['time' => microtime(true) - $this->end_time]);
            },

        ];
    }

    /**
     * @return int
     */
    public function chunkSize(): int {
        // TODO: Implement chunkSize() method.
        return 3000;
    }
}
